package com.china.adapter;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.china.osourcechina.R;
import com.china.utils.AcquisitionTime;
import com.china.vo.Tweet;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/5/10.
 */
public class TweetAdapter extends BaseAdapter {

    private List<Tweet> list;
    private Context context;
    private ImageLoader imageLoader = ImageLoader.getInstance();

    public TweetAdapter(List<Tweet> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder h;
        if(convertView==null){
            h = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.fragment_tweet_item,null);
            h.iv_tweet_item_uimg = (ImageView) convertView.findViewById(R.id.iv_tweet_item_uimg);
            h.iv_tweet_item_img = (ImageView) convertView.findViewById(R.id.iv_tweet_item_img);
            h.tv_tweet_item_uname = (TextView) convertView.findViewById(R.id.tv_tweet_item_uname);
            h.tv_tweet_item_title = (TextView) convertView.findViewById(R.id.tv_tweet_item_title);
            h.ll_tweet_item_praise = (LinearLayout) convertView.findViewById(R.id.ll_tweet_item_praise);
            h.tv_tweet_item_time = (TextView) convertView.findViewById(R.id.tv_tweet_item_time);
            h.tv_tweet_item_comment = (TextView) convertView.findViewById(R.id.tv_tweet_item_comment);
            convertView.setTag(h);
        }else {
            h = (ViewHolder) convertView.getTag();
        }
        if(list.get(position).getPortrait().equals("")){
            h.iv_tweet_item_uimg.setImageResource(R.mipmap.widget_dface);
        }else {
            imageLoader.displayImage(list.get(position).getPortrait(),h.iv_tweet_item_uimg);
        }
        h.ll_tweet_item_praise.removeAllViews();
        if(Integer.parseInt(list.get(position).getLikeCount())!=0){
            for(int i = 0;i<list.get(position).getLikeList().getUser().size();i++){
                if(i<4){
                    TextView textView = new TextView(context);
                    textView.setText(list.get(position).getLikeList().getUser().get(i).getName()+"、");
                    textView.setTextColor(context.getResources().getColor(R.color.Indigo_colorPrimary));
                    textView.setTextSize(12);
                    h.ll_tweet_item_praise.addView(textView);
                }
            }
            TextView textView = new TextView(context);
            textView.setText(Html.fromHtml("等"+"<font color=#40aa53>"+list.get(position).getLikeCount()+"人</font>"+"觉得很赞"));
            textView.setTextSize(12);
            h.ll_tweet_item_praise.addView(textView);
        }
        h.tv_tweet_item_comment.setText(list.get(position).getCommentCount());
        h.tv_tweet_item_uname.setText(list.get(position).getAuthor());
        h.tv_tweet_item_title.setText(list.get(position).getBody());
        h.tv_tweet_item_time.setText(AcquisitionTime.getStandardDate(AcquisitionTime.dateToTimestamp(list.get(position).getPubDate())));
        if(list.get(position).getImgSmall()!=""){
            h.iv_tweet_item_img.setVisibility(View.VISIBLE);
            imageLoader.displayImage(list.get(position).getImgSmall(),h.iv_tweet_item_img);
        }else {
            h.iv_tweet_item_img.setVisibility(View.GONE);
        }
        return convertView;
    }

    class ViewHolder{
        ImageView iv_tweet_item_uimg,iv_tweet_item_img;
        TextView tv_tweet_item_uname,tv_tweet_item_title,tv_tweet_item_time,tv_tweet_item_comment;
        LinearLayout ll_tweet_item_praise;
    }
}
